/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "rtc.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "usb_device.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "lcd_extern.h"
#include "W25QXX.h"
#include "gpio_extern.h"
#include "debug_button_extern.h"
#include "usart_extern.h"
#include "finger_sener.h"
#include "TastekE16.h"
#include "door_manager.h"
#include "data_center.h"
#include "string_library.h"
#include "interaction_handle.h"
#include "rader.h"
#include "sound.h"
#include "Warner_Manager.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void) {
    /* USER CODE BEGIN 1 */

    /* USER CODE END 1 */

    /* MCU Configuration--------------------------------------------------------*/

    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();

    /* USER CODE BEGIN Init */

    /* USER CODE END Init */

    /* Configure the system clock */
    SystemClock_Config();

    /* USER CODE BEGIN SysInit */

    /* USER CODE END SysInit */

    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_DMA_Init();
    MX_SPI1_Init();
    MX_USB_DEVICE_Init();
    MX_UART5_Init();
    MX_USART1_UART_Init();
    MX_USART2_UART_Init();
    MX_USART3_UART_Init();
    MX_SPI3_Init();
    MX_RTC_Init();
    MX_TIM6_Init();
    MX_TIM16_Init();
    MX_UART4_Init();
    MX_TIM2_Init();
    /* USER CODE BEGIN 2 */
    DoorManagerInit();
    HAL_TIM_Base_Start_IT(&htim6);
    WarnerManagerInit();
    Extern_LCD_Init();
    LCD_Extern_initProcessHandle(80, 260, 32, 320);
    LCD_Extern_setProcessTitle("LCD Screen", 10);
    LCD_Extern_setProcessTitle("LED", 20);
    LED_R_SET;
    LED_G_SET;
    LED_B_SET;
    LED_R_RESET;
    LED_G_RESET;
    LED_B_RESET;

    /**
     * W25X Flash
     */
    LCD_Extern_setProcessTitle("Flash Initing", 30);
    SPI_W25X_WAKEUP();
    HAL_Delay(100);
    if (SPI_W25X_ReadID() == 0xEF16)
        LCD_Extern_setProcessTitle("ID:W25Q64", 40);
    else if (SPI_W25X_ReadID() == 0xEF17)
        LCD_Extern_setProcessTitle("ID:W25Q128", 40);
    else if (SPI_W25X_ReadID() == 0xEF15)
        LCD_Extern_setProcessTitle("ID:W25Q32", 40);
    else {
        LCD_Extern_setProcessTitle("Flash_Err", 0);
        Error_Handler();
    }
    LCD_Extern_setProcessTitle("Done", 40);
    RADAR_POWER_ON;
    /**
     * USART Load
     */
    LCD_Extern_setProcessTitle("Usart DMA", 50);
    EX_DMA_Receive_Init();
    LCD_Extern_setProcessTitle("Done", 55);
    /**
     * FingerSensor
     */
    LCD_Extern_setProcessTitle("FingerSensor.", 56);
    initFingerSensor();
    LCD_Extern_setProcessTitle("FingerSensor..", 57);
    while (isIniting() == 1);
    setRedLED();
    LCD_Extern_setProcessTitle("FingerSensor...", 58);
    HAL_Delay(100);
    setBlueLED();
    LCD_Extern_setProcessTitle("FingerSensor....", 59);
    HAL_Delay(100);
    setPurpleLED();
    LCD_Extern_setProcessTitle("FingerSensor Done", 60);
/**
 *  LCD_Extern_setProcessTitle("Flash Writing", 75);
    SPI_W25X_Write(gImage_DaiJi,0,57600);
    LCD_Extern_setProcessTitle("Flash Write Done", 90);
        LCD_Extern_setProcessTitle("2GModel", 70);

 */
    LCD_Extern_setProcessTitle("2GModel", 65);
    Tas_init();
    LCD_Extern_setProcessTitle("2GModel Done", 76);
    if (isConnected()) {
        LCD_Extern_setProcessTitle("SyncTime", 77);
        reuqest_time();
        uint8_t state = 0;
        while (1) {
            state = tas_time_not_synced();
            if (state == 1) {
                LCD_Extern_setProcessTitle("Sync Done", 78);
                break;
            }
            if (state == 2) {
                LCD_Extern_setProcessTitle("Sync Timeout", 78);
                break;
            }
        }
    } else {
        LCD_Extern_setProcessTitle("Offline Ignored", 78);
    }
    /**
     * System Logger
     */
    LCD_Extern_setProcessTitle("System Logger", 80);
    system_logger_init();
    addEvent(EVENT_TYPE_ONLINE);
    uint8_t data[6];
    myitoa(getIndex(), data, 10);
    debug_sync("[DEBUG]SYSTEM logger amount:");
    debug_sync(data);
    debug_sync("\n");
    loadConfig();
    enableAutoSet();
    setStartTime(8, 20, 41);
    setEndTime(8, 40, 1);
    LCD_Extern_setProcessTitle("LoadDone", 89);
    /**
     * button register
     */
    LCD_Extern_setProcessTitle("Button Register:", 91);
    button_init();
    buttonRegister(0, 500, 1, 1, 100);
    buttonRegister(1, 500, 1, 1, 100);
    buttonRegister(2, 500, 1, 1, 100);
    buttonRegister(3, 500, 1, 1, 100);



    LCD_Extern_setProcessTitle("Done", 93);
    /**
     * Done render
     */
    LCD_Extern_setProcessTitle("Kernel Init done", 100);
    LCD_Extern_clearProcessHandle();
    //  listLog();
    kernal_init_done();
    EX_Interact_Handle_Init();


    setVOL(25);
    //  playSound(1);
    HAL_TIM_Base_Start_IT(&htim16);
    verifyTimeSynced();

    setDoorIcon(0);
    HAL_Delay(1000);
    setDoorIcon(1);

    while (1) {
        // HAL_UART_Transmit(&huart4,"ERR",3,10);
        onSoundMainThreadExecute();
        Interact_Handle_Main_Thread_Execute();
        cycle_read_GPIOB_1();
        cycle_read_GPIOA_1();
        cycle_read_GPIOB_2();
        cycle_read_GPIOB_0();
    }
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1) {
        /* USER CODE END WHILE */

        /* USER CODE BEGIN 3 */
    }
    /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void) {
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
    RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

    /** Initializes the RCC Oscillators according to the specified parameters
    * in the RCC_OscInitTypeDef structure.
    */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL3;
    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
        Error_Handler();
    }
    /** Initializes the CPU, AHB and APB buses clocks
    */
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
                                  | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
        Error_Handler();
    }
    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB | RCC_PERIPHCLK_USART1
                                         | RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_USART3
                                         | RCC_PERIPHCLK_UART4 | RCC_PERIPHCLK_UART5
                                         | RCC_PERIPHCLK_RTC;
    PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
    PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
    PeriphClkInit.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;
    PeriphClkInit.Uart4ClockSelection = RCC_UART4CLKSOURCE_PCLK1;
    PeriphClkInit.Uart5ClockSelection = RCC_UART5CLKSOURCE_PCLK1;
    PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV32;
    PeriphClkInit.USBClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
        Error_Handler();
    }
}

/* USER CODE BEGIN 4 */
void debug(char *data) {
    uint8_t i = 0;
    while (*(data + i++) != '\0' && i != 255);
    i--;
    HAL_UART_Transmit_DMA(&huart1, (uint8_t *) data, i);
}

void debug_sync(char *data) {
    uint8_t i = 0;
    while (*(data + i++) != '\0' && i != 255);
    i--;
    HAL_UART_Transmit(&huart1, (uint8_t *) data, i, 0xFF);
}
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void) {
    /* USER CODE BEGIN Error_Handler_Debug */
    /* User can add his own implementation to report the HAL error return state */
    __disable_irq();
    while (1) {
    }
    /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
